<?php
/**
 * Created by PhpStorm.
 * User: Kau
 * Date: 2018/3/21
 * Time: 1:08
 */
namespace app\bis\controller;

use app\common\facade\Bis;
use app\common\facade\BisAccount;
use app\common\facade\BisLocation;
use app\common\facade\Category;
use app\common\facade\City;
use think\Controller;
use think\Request;

class Location extends Base
{
    public function index(Request $request)
    {
        $bisId = $request->param('bis_Id','1', 'intval');
        $locations = model('BisLocation')->getFirstLocation($bisId);

        $this->assign('locations', $locations);
        return $this->fetch();
    }

    public function add(Request $request)
    {
        if ($request->isPost())
        {
            $data = $request->post();
            // TODO：数据校验

            $bisId = $this->getLoginUser()->bis_id;
            $data['cat'] = '';
            if (!empty($data['se_category_id']))
            {
                $data['cat'] = implode('|', $data['se_category_id']);
            }

            // 获取经纬度
            $lnglat = \Map::getLngLat($data['address']);
            //if (empty($lnglat) || $lnglat['status'] != 0 || $lnglat['result']['precise'] != 1)
            if (empty($lnglat) || $lnglat['status'] != 0)
            {
                $this->error('无法获取数据，或者匹配的地址不精确');
            }
            // 门店入库
            $locationData = [
                'bis_id' => $bisId,
                'name' => $data['name'],
                'logo' => $data['logo'],
                'tel' => $data['tel'],
                'contact' => $data['contact'],
                'category_id' => $data['category_id'],
                'category_path' => $data['category_id'] . ',' . $data['cat'],
                'city_id' => $data['city_id'],
                'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id']. ',' . $data['se_city_id'],
                'api_address' => $data['address'],
                'open_time' => $data['open_time'],
                'content' => empty($data['content']) ? '' : $data['content'],
                'is_main' => 0,
                'xpoint' => empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
                'ypoint' => empty($lnglat['result']['location']['lat']) ? '' : $lnglat['result']['location']['lat'],
            ];
            $locationId = model('BisLocation')->add($locationData);
            if ($locationId)
            {
                $this->success('门店申请成功');
            }
            else
            {
                $this->error('门店申请失败');
            }

        }
        // 获取一级城市数据
        $citys = model('City')->getNormalCitysByparentId();
        // 获取一级栏目数据
        $categorys = model('Category')->getNormalCategorysByparentId();
        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys
        ]);
        return $this->fetch();
    }

    public function detail(Request $request)
    {
        $id = $request->param('id');
        if (empty($id))
        {
            return $this->error('ID错误');
        }
        // 获取一级城市数据
        $citys = model('City')->getNormalCitysByparentId();
        // 获取一级栏目数据
        $categorys = model('Category')->getNormalCategorysByparentId();
        // 获取商户数据
        $bisData = model('Bis')->get($this->account['bis_id']);
        $locationData = model('BisLocation')->get(['id' => $id]);
        $accountData = model('BisAccount')->get(['id' => $this->account['id']]);
        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys,
            'bisData' => $bisData,
            'locationData' => $locationData,
            'accountData' => $accountData
        ]);
        return $this->fetch();
    }

    public function delete(Request $request)
    {
        $id = $request->param('id');
        $res = model('BisLocation')->save(['status' => -1], ['id' => $id]);
        if ($res)
        {
            $this->success('下架成功');
        }
        else
        {
            $this->error('下架失败');
        }
    }
}